package core;

import infrastructure.exceptions.LoginException;
import infrastructure.exceptions.operations.OperationException;
import remote.Session;
import remote.SessionManager;
import server.IServerFacade;
import server.IServerSession;
import authentication.AuthenticationManager;
import dal.User;

public class ServerFacade implements IServerFacade {

	private Session userSession;
	
	public ServerFacade(Session userSession){
		this.userSession = userSession; 
	}
	
	public User getLoggedInUser() throws OperationException {		
		return userSession.getUser();
	}	

	public IServerSession logon(String userName,String password) throws LoginException {
		logout();
		
		// try to authenticate the user
		userSession = AuthenticationManager.getInstance().authenticate(userName,password);
				
		// Check if the user is an administrator user 
		if (AuthenticationManager.getInstance().isAdministrator(userSession.getUser())){
			return new AdminInterface(userSession);
		}else if (!AuthenticationManager.getInstance().isGuestUser(userSession.getUser())){
			return new ServerSession(userSession);
		}else{
			throw new LoginException(userName, password);
		}
	}

	public void logout() {
		// Delete session from the session manager
		SessionManager.getInstance().deleteSession(userSession.getSessionID());
	}
	
	
	

}
